package cn.edu.sdjzu.xg.eagle.service.impl;

import cn.edu.sdjzu.xg.eagle.dao.StudentDao;
import cn.edu.sdjzu.xg.eagle.entity.ActorType;
import cn.edu.sdjzu.xg.eagle.entity.Student;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.factory.DaoFactory;
import cn.edu.sdjzu.xg.eagle.factory.ServiceFactory;
import cn.edu.sdjzu.xg.eagle.service.StudentService;
import cn.edu.sdjzu.xg.eagle.service.UserService;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.IdUtil;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;
import cn.edu.sdjzu.xg.eagle.util.Pagination;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * @author tg.si@188.com
 * @project xg23
 * @date 2025/3/28
 */
public class StudentServiceImpl implements StudentService {
    //    static ActorService actorService = null;
    static StudentDao StudentDao = null;
    static UserService userService = null;
    static {
        try{
            StudentDao = DaoFactory.getDao(StudentDao.class,"StudentDaoImpl");
//            actorService = ServiceFactory.getService(ActorService.class, "ActorServiceImpl");
            userService = ServiceFactory.getService(UserService.class, "UserServiceImpl");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean add(Student Student) throws SQLException, BaseException,
            InvocationTargetException {
        Connection connection = null;
        try {
            connection = JdbcHelper.getConn();
            connection.setAutoCommit(false);

            Long StudentId = IdUtil.getLongId();
            Student.setId(StudentId);

            User user = new User();
            user.setUsername(Student.getNo());
            user.setPassword(Student.getNo());

            user.setActorType(ActorType.Student);



            user.setActor(Student);
            Integer userId = userService.add(connection, user);
            if (userId == null) {
                throw new BaseException("添加用户失败.");
            }

            user.setId(userId);

            Student.setUser(user);

            boolean isAdded = StudentDao.add(connection, Student);
            if (!isAdded) {
                throw new BaseException("添加学生失败.");
            }

            connection.commit(); // 提交事务
            return true;
        } catch (SQLException | BaseException | InvocationTargetException e) {
            if (connection != null) {
                connection.rollback(); // 回滚事务
            }
            e.printStackTrace();
            throw new BaseException("数据库操作异常.");
        } finally {
            JdbcHelper.close(connection); // 关闭数据库连接
        }
    }


    @Override
    public Student find(Long id) throws SQLException, BaseException, InvocationTargetException {
        Connection connection = JdbcHelper.getConn();
        Student Student = this.find(connection, id);
        JdbcHelper.close(connection);
        return Student;
    }

    @Override
    public Student find(Connection connection, Long id) throws SQLException, BaseException, InvocationTargetException {
        return StudentDao.find(connection, id);
    }

    @Override
    public Collection<Student> findAll(List<Condition> conditionList, Pagination pagination) throws SQLException, BaseException, InvocationTargetException {
        //获得连接以便完成可能的事务操作
        Connection connection = JdbcHelper.getConn();
        Collection<Student> Students = StudentDao.findAll
                (connection, conditionList, pagination);
        JdbcHelper.close(connection);
        return Students;
    }
}
